from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
# 就是普通的函数，但是得有返回值
from book.models import BookInfo


def index(request):
    books = BookInfo.objects.all()
    print(books)
    return HttpResponse(books.all())  # 在index页面进行返回


from book.models import BookInfo

# 方式一
book = BookInfo(
    name='Django',
    pub_date='1001-1-1',
    readcount=10
)
# 必须使用book.save()才能将数据保存在数据库中

# 方式二
# objects -- 相当于一个代理，实现增删查改
BookInfo.objects.create(
    name='linux',
    pub_date='2001-1-1',
    readcount=10
)
# 修改数据
# 方式一:
book = BookInfo.objects.get(id=6)
book.name = 'linux入门'
book.save()  # 保存数据到数据库
# 方式二
BookInfo.objects.filter(id=6).update(name='爬虫入门', commentcount=100)

# 删除数据
# 1.物理删除 删除这条数据
# 2.逻辑删除 删除标记位
book = BookInfo.objects.get(id=6)
book.delete()
# 查询
'''
get 查询一个结果，不存在会抛异常，DoesNotExist
all 查询多个结果
count 查询数量
'''
BookInfo.objects.get(id=3)
BookInfo.objects.all()
BookInfo.objects.count(name='')

from django.db.models import F

# 说明：2个属性之间的比较
# 语法格式：模型类名.objects.filter(属性名__运算符=F('第二个属性名'))
# 查询阅读量大于等于评论量的书籍
# from django.db.models import F
# BookInfo.objects.filter(readcount__gte=F('commentcount'))

from book.models import PeopleInfo
from django.db.models import Sum, Max, Min, Avg, Count
BookInfo.objects.aggregate(Sum('readcount'))

book=BookInfo.objects.get(id=1)
book.peopleinfo_set.all()
from book.models import BookInfo
PeopleInfo.objects.filter(book__readcount__gt=80)
from django.core.paginator import Paginator
books = BookInfo.objects.all()
paginator = Paginator(books,2).num_pages
total_data = paginator.page(1)